﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;

namespace DataAccess
{
    public class ClientDao
    {
        DataAccessLayer dataAccessLayer=new DataAccessLayer();

        public DataTable GetAllClient()
        {
            return
                dataAccessLayer.GetDataTable(
                    "Select ClientId,UserName,CompanyName,Address,PhoneNumber,Email From tblClients");
        }
        public DataTable GetClientById(string id)
        {
            return
                dataAccessLayer.GetDataTable(
                    "Select ClientId,UserName,CompanyName,Address,PhoneNumber,Email From tblClients Where ClientId='" +
                    id + "'");
        }
        public int InsertClient(Client client)
        {
            try
            {
                var parameterList = new Collection<SqlParameter>
                                        {
                                           new SqlParameter("@UserName",client.UserName),
                                           new SqlParameter("@CompanyName", client.CompanyName),
                                           new SqlParameter("@Address", client.Address),
                                           new SqlParameter("@PhoneNumber", client.PhoneNumber),
                                           new SqlParameter("@Email",client.Email)
                                        };
                return dataAccessLayer.ExecuteNonQuery("InsertClient", parameterList);
            }
            catch (SqlException)
            {
                return 0;
            }
        }
        public int UpdateClient(Client client)
        {
            try
            {
                var parameterList = new Collection<SqlParameter>
                                        {
                                            new SqlParameter("@ClientId",client.ClientId),
                                           new SqlParameter("@UserName",client.UserName),
                                           new SqlParameter("@CompanyName", client.CompanyName),
                                           new SqlParameter("@Address", client.Address),
                                           new SqlParameter("@PhoneNumber", client.PhoneNumber),
                                           new SqlParameter("@Email",client.Email)
                                        };
                return dataAccessLayer.ExecuteNonQuery("UpdateClient", parameterList);
            }
            catch (SqlException)
            {
                return 0;
            }
        }
        public int DeleteClient(Client client)
        {
            try
            {
                var parameterList = new Collection<SqlParameter>
                                        {
                                            new SqlParameter("@ClientId", client.ClientId)
                                        };
                return dataAccessLayer.ExecuteNonQuery("DeleteClient", parameterList);
            }
            catch (SqlException exception)
            {
                return 0;
            }
        }
    }
}
